<template>
  <div class="coupon-page" v-loading="loading" element-loading-text="加载中...">
    <div class="coupon-title">
      <div class="coupon-title-left">
        <el-form inline>
          <el-form-item>
            <el-input
              v-model="searchForm.name"
              placeholder="优惠券名称"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-select v-model="searchForm.status" clearable placeholder="状态">
              <el-option label="有效" :value="1" />
              <el-option label="无效" :value="0" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchFun">查询</el-button>
            <el-button @click="resetFun">重置</el-button>
          </el-form-item>
        </el-form>
        <!-- <span style="vertical-align: middle"> 优惠券1</span> -->
      </div>
      <div class="coupon-title-right">
        <el-button type="primary" :icon="Plus" @click="addDialogAutoFun('add')">
          自动赠券设置
        </el-button>
        <el-button type="primary" :icon="Plus" @click="addDeptFun('add')">
          添加
        </el-button>
      </div>
    </div>
    <div class="coupon-table">
      <el-table
        border
        stripe
        :data="tableData"
        :height="store.tableHeight - 40"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="coupon_name" label="优惠券名称" width="155" />
        <el-table-column
          prop="coupon_remark"
          label="优惠券规则描述"
          width="200"
        >
        </el-table-column>
        <el-table-column prop="coupon_price" label="优惠券面值" width="100">
        </el-table-column>
        <el-table-column prop="use_min_price" label="最低消费" width="100" />
        <el-table-column prop="total_count" label="发放数量" width="100" />
        <el-table-column prop="remain_count" label="剩余领取数量" width="120" />
        <el-table-column
          prop="receive_limit"
          label="领取的优惠券数量"
          width="140"
        />
        <el-table-column prop="receive_type" label="领取方式" width="100">
          <template #default="{ row }">
            <el-tag v-if="row.receive_type == 1">手动领取</el-tag>
            <el-tag v-else-if="row.receive_type == 2" type="success">
              后台发放
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column
          prop="reg_start_time"
          label="注册开始时间"
          width="170"
        />
        <el-table-column prop="reg_end_time" label="注册结束时间" width="170" />
        <el-table-column prop="start_time" label="领取开启时间" width="170" />
        <el-table-column prop="end_time" label="领取结束时间" width="170" />
        <el-table-column
          prop="start_use_time"
          label="优惠券使用开始时间"
          width="170"
        />
        <el-table-column
          prop="end_use_time"
          label="优惠券使用结束时间"
          width="170"
        />
        <el-table-column prop="goods_type" label="适用商品类型" width="130">
          <template #default="{ row }">
            <el-tag v-if="row.goods_type == 1">全部商品可用</el-tag>
            <el-tag v-else-if="row.goods_type == 2" type="success">
              指定商品可用
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="sort" label="排序" width="70" />
        <el-table-column prop="status" label="状态" width="90">
          <template #default="{ row }">
            <el-switch
              v-model="row.status"
              active-text="有效"
              :active-value="1"
              inactive-text="无效"
              :inactive-value="0"
              inline-prompt
              @change="statusFun(row)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" width="180">
        </el-table-column>

        <el-table-column label="操作" width="190" align="center" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="updateOpen(row)"
              >编辑</el-button
            >
            <el-button type="success" link @click="toLog(row)"
              >领取记录</el-button
            >

            <el-button type="warning" link @click="reBackFun(row)"
              >撤回</el-button
            >
            <el-button type="danger" link @click="delFun(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-div">
        <div>
          <el-button
            type="danger"
            :disabled="selectData.length == 0 ? true : false"
            @click="delAllFun"
          >
            <template #icon>
              <IEpDelete />
            </template>
            批量删除</el-button
          >
        </div>
        <el-pagination
          v-model:currentPage="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[20, 50, 100, 200]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
        <div></div>
      </div>
    </div>
  </div>
  <addDialog ref="addDialogRef" @refreshList="refreshList" />
  <addDialogAuto ref="addDialogAutoRef" @refreshList="refreshList" />
</template>
<script setup>
import { Plus } from "@element-plus/icons-vue";
import { useCounterStore } from "@/stores/counter.js";
import {
  listApi,
  delApi,
  reCallApi,
  stateApi,
} from "@/plugins/api/shopping/coupon";
import addDialog from "./components/DialogCoupon.vue";
import addDialogAuto from "./components/DialogCouponAuto.vue";
const store = useCounterStore();
const router = useRouter();
let searchForm = ref({});
function searchFun() {
  currentPage.value = 1;
  selectData.value = [];
  tableData.value = [];
  getData();
}
function resetFun() {
  currentPage.value = 1;
  searchForm.value = {};
  tableData.value = [];
  selectData.value = [];
  getData();
}
let tableData = ref([]);
let loading = ref(false);
const currentPage = ref(1);
const pageSize = ref(20);
const total = ref(0);
const handleSizeChange = (val) => {
  pageSize.value = val;
  getData();
};
const handleCurrentChange = (val) => {
  currentPage.value = val;
  getData();
};

function getData() {
  loading.value = true;
  let obj = Object.assign(
    {
      page: currentPage.value,
      pageSize: pageSize.value,
    },
    searchForm.value
  );
  listApi(obj)
    .then((data) => {
      tableData.value = data.list;
      total.value = data.page.total;
      loading.value = false;
    })
    .catch(() => {
      loading.value = false;
    });
}
let addDialogRef = ref(null);
let addDialogAutoRef = ref(null);
function addDeptFun(type) {
  addDialogRef.value.initFun(type);
}
function addDialogAutoFun(type) {
  addDialogAutoRef.value.initFun(type);
}
function updateOpen(row) {
  addDialogRef.value.initFun("update", row);
}
function refreshList() {
  getData();
}
let selectData = ref([]);
function handleSelectionChange(val) {
  selectData.value = val;
}
function reBackFun(row) {
  ElMessageBox.confirm("确认撤回，是否继续?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      reCallApi({
        id: row.id.toString(),
      }).then(() => {
        ElMessage({
          type: "success",
          message: "撤回成功！",
        });
        getData();
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消。",
      });
    });
}
function delFun(row) {
  ElMessageBox.confirm("确认删除，是否继续?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delApi({
        id: row.id.toString(),
      }).then(() => {
        ElMessage({
          type: "success",
          message: "删除成功！",
        });
        getData();
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消。",
      });
    });
}

function delAllFun() {
  ElMessageBox.confirm("确认批量删除选中的数据，是否继续?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      let arr = selectData.value.map((ele) => {
        return ele.id;
      });
      delApi({
        id: arr.join(","),
      }).then(() => {
        ElMessage({
          type: "success",
          message: "删除成功！",
        });
        selectData.value = [];
        getData();
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消。",
      });
    });
}

function statusFun(row) {
  stateApi({
    status: row.status,
    id: row.id,
  }).then(() => {
    ElMessage({
      type: "success",
      message: "修改成功！",
    });
    getData();
  });
}
function toLog(row) {
  router.push({
    path: "/shopping/coupon/log",
    query: {
      couponId: row.id,
    },
  });
}
onMounted(() => {
  getData();
});
</script>
<style scoped lang="scss">
.coupon-page {
  width: 100%;
  height: 100%;

  .coupon-title {
    width: 100%;
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #eeeeee;
    padding-bottom: 10px;

    .coupon-title-left {
      font-size: 18px;
      font-weight: 700;
      color: #172b4d;

      :deep(.el-form-item) {
        margin-bottom: 0;
      }
    }
  }

  .coupon-table {
    padding: 10px;
    box-sizing: border-box;

    .pagination-div {
      display: flex;
      justify-content: space-between;
      margin-top: 10px;
    }
  }
}
</style>
